package com.linln.common.utils;

import org.springframework.web.multipart.MultipartFile;
import javax.activation.MimetypesFileTypeMap;
import java.io.*;
import java.util.UUID;

/**
 * Created by Administrator on 2020-02-17.
 */
public class FileUtil {
    /**
     * MultipartFile 转 File
     * @param file
     * @throws Exception
     */
    public static void multipartFileToFile(MultipartFile file ) throws Exception {

        File toFile = null;
        if(file.equals("")||file.getSize()<=0){
            file = null;
        }else {
            InputStream ins = null;
            ins = file.getInputStream();
            toFile = new File(file.getOriginalFilename());
            inputStreamToFile(ins, toFile);
            ins.close();
        }

    }

    /**
     * File 转 MultipartFile
     * @param file
     * @throws Exception
     */
//    public static void fileToMultipartFile( File file ) throws Exception {
//
//        FileInputStream fileInput = new FileInputStream(file);
//        String contentType=FileUtil.getContentType(file.getPath());
//        MultipartFile toMultipartFile = new MockMultipartFile("file",file.getName(),contentType,fileInput);
//        toMultipartFile.getInputStream();
//
//    }


    public static void inputStreamToFile(InputStream ins, File file) {
        try {
            OutputStream os = new FileOutputStream(file);
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.close();
            ins.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取文件contentType
     * 该方式支持本地文件，也支持http/https远程文件
     */
    public static String getContentType(File file) {
        return FileUtil.getContentType(file.getPath());
    }

    /**
     * 获取文件contentType
     * 该方式支持本地文件，也支持http/https远程文件
     * @param fileUrl
     * @return
     */
    public static String getContentType(String fileUrl) {
        String contentType = null;
        try {
            contentType = new MimetypesFileTypeMap().getContentType(new File(fileUrl));
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("getContentType, File ContentType is : " + contentType);
        return contentType;
    }
    /**
     * 生成随机且唯一的文件名
     */
    public static String genFileName(String originalFilename){
        String fileSuffix = ToolUtil.getFileSuffix(originalFilename);
        return UUID.randomUUID().toString().replace("-", "") + fileSuffix;
    }
    /***
     * 判断文件夹中是否存在改文件，存在则删除该文件
     * @param dirPath
     * @param fileName
     */
    public static void existsDelete(String dirPath,String fileName) {
        File pathFile = new File(dirPath);
        if(!pathFile.exists() || pathFile.isFile()) {
            return;
        }
        for(File file:pathFile.listFiles()) {
            if(file.isFile() && fileName.equals(file.getName())) {
                file.delete();
                break;
            }
        }
    }


}
